#include <signal.h>
#include <unistd.h>
#include <iostream>

using std::cout;
using std::endl;
bool loop = true;

void Print(sigset_t &pending)
{
    for (int sig = 31; sig > 0; sig--) // 没有0号信号，信号的范围1——31，两闭
    {
        if (sigismember(&pending, sig))
        {
            cout << 1;
        }
        else
        {
            cout << 0;
        }
    }
    cout << endl;
}
void donesig2(int sig)
{
    cout << "get sig 3" << endl;
    cout << "loop = false, done sig2" << endl;
    loop = false;
}
void sigcb(int sig)
{
    loop = true;
    cout << "get a sig:" << sig << endl;
    while (loop)
    {
        sigset_t pending;
        sigpending(&pending);
        Print(pending);
        sleep(1);
        signal(3, donesig2);
    }
}

int main()
{
    struct sigaction ac, oac;
    ac.sa_flags = 0;
    sigemptyset(&ac.sa_mask);
    ac.sa_handler = sigcb;

    while (true)
    {
        sigaction(2, &ac, &oac);
        sleep(1);
        cout << "I am process:" << getpid() << endl;
    }

    return 0;
}
